/*
 * Validator.java
 */
package util;


import java.awt.*;
import javax.swing.*;
import java.lang.*;
import java.util.*;

/**
 *
 * @author suzann
 */
public class Validator 
{
    /**
     * Validate range, only for JTextField
     * @param comps List of Component objects to be validated
     * @param min a double value of minimum value
     * @param max a double value of maximum value
     */
    public static boolean validateRange(ArrayList <Component> comps, double min, double max)
    {
        boolean range = true, format = true, isblank = false;
        String msg = "";
        
        for (int i = 0; i < comps.size(); i++ )
        {
            Component c = (Component) comps.get(i);
            
            if (c instanceof JTextField)
            {
                JTextField jt = (JTextField)c;
                String text = jt.getText();

                if(text==null || text.equals(""))
                {
                	// is blank 
                	return false;
                }
                 
                try
                {
                    Double compare = Double.parseDouble(jt.getText());
                    if (compare > max || compare < min)
                    {
                        range = false;
                    }
                }
                catch(Exception ex)
                {
                    System.out.println(ex);
                    format = false;
                }
            }
            else
            {
                return false;
            }
        }
        
        if (range == false)
        {
            msg += "Value has to be in between " + min + " and " + max + ".";
        }
        
        if (format == false)
        {
            msg += "\nPlease enter a valid decimal value";
        }
        
        if (range == false || format == false)
        {
            JOptionPane.showMessageDialog(null, msg, "Validation Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        else
            return true;
    }
    
    /**
     * Validate range, only for JSpinner
     * @param comps List of Component objects to be validated
     * @param min a double value of minimum value
     * @param max a double value of maximum value
     */
    public static boolean validateRange_Spinner(ArrayList <Component> comps, double min, double max)
    {
        boolean range = true, format = true, isblank = false;
        String msg = "";
        
        for (int i = 0; i < comps.size(); i++ )
        {
            Component c = (Component) comps.get(i);
            
            if (c instanceof JSpinner)
            {
            	JSpinner js = (JSpinner)c;
                String text = js.getValue().toString();
                
                if(text==null || text.equals(""))
                {
                	// is blank 
                	return false;
                }
                
                try
                {
                    Double compare = Double.parseDouble(js.getValue().toString());
                    if (compare > max || compare < min)
                    {
                        range = false;
                    }
                }
                catch(Exception ex)
                {
                    System.out.println(ex);
                    format = false;
                }
            }
            else
            {
                return false;
            }
        }
        
        if (range == false)
        {
            msg += "Value has to be in between " + min + " and " + max + ".\n";
        }
        
        if (format == false)
        {
            msg += "Please enter a valid decimal value\n";
        }
        
        if (range == false || format == false)
        {
            JOptionPane.showMessageDialog(null, msg, "Validation Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        else
            return true;
    }
    
    
     /**
     * Validate blank, only for JTextField
     * @param comps List of Component objects to be validated
     */
    public static boolean validateBlank(ArrayList <Component> comps)
    {
        boolean isblank = false;
        String msg = "";
        
        for (int i = 0; i < comps.size(); i++ )
        {
            Component c = (Component) comps.get(i);
            if (c instanceof JTextField)
            {
                JTextField jt = (JTextField)c;
                String text = jt.getText();
                if (text.equals("") || text == null)
                    isblank = true;      
            }
            
        }
        
        if (isblank)
        {
            msg += "Please fill in all the values.";
            JOptionPane.showMessageDialog(null, msg, "Validation Error", JOptionPane.ERROR_MESSAGE);
        }
        
        return isblank;
    }
    
    /**
     * Validate blank, only for JSpinner
     * @param comps List of Component objects to be validated
     */
    public static boolean validateBlank_Spinner(ArrayList <Component> comps)
    {
        boolean isblank = false;
        String msg = "";
        
        for (int i = 0; i < comps.size(); i++ )
        {
            Component c = (Component) comps.get(i);
            if (c instanceof JSpinner)
            {
            	JSpinner js = (JSpinner)c;
                String text = js.getValue().toString();
                if (text.equals("") || text == null)
                    isblank = true;      
            }
            
        }
        
        if (isblank)
        {
            msg += "Please fill in all the values.";
            JOptionPane.showMessageDialog(null, msg, "Validation Error", JOptionPane.ERROR_MESSAGE);
        }
        
        return isblank;
    }


}
